package rocketmq.spring;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RocketProducer {
    @Autowired
    private DefaultMQProducer producer;

    public void send(String msg) {
        try {
            Message message = new Message("TopicTest", "TagA", msg.getBytes(RemotingHelper.DEFAULT_CHARSET));
            // 这里调用的是同步的方式，所以会有返回结果
            SendResult sendResult = producer.send(message);
            System.out.println(sendResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
